package cn.vshare.aop;


import cn.vshare.entity.Comment;
import cn.vshare.util.SensitiveWordUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;


/**
 * @Auther: Administrator
 * @Date: 2018/10/8 20:23
 * @Description:
 */
@Component
@Aspect
@EnableAspectJAutoProxy
public class SensitiveWordAop {

    @Pointcut("execution(* cn.vshare.service.CommentService.add*(..))")
    public void pointCut() {
    }


    @Around("pointCut()")
    public Comment checkCommentSensitiveWord(ProceedingJoinPoint joinPoint) throws Throwable {
        //System.out.println("========================>前置通知");
        Comment comment=null;
        Object[] objs = joinPoint.getArgs();
        String context = (String) objs[2];
        int count = SensitiveWordUtil.checkBadWord(context, 0, 2);
        //System.out.println("========================>原来是啥啊?" + context);
        if (count > 0) {
            context = SensitiveWordUtil.replaceBadWord(context, 2, "*");
            //System.out.println("=======================>修改了吗???" + context);
            objs[2] = context;
        }
        comment= (Comment) joinPoint.proceed(objs);
        return comment;
    }


}
